<?php

namespace addons\shop\controller;

use addons\shop\model\Train as M;
use think\Db;
use think\Validate;

class Train extends Base
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function list()
    {
        $type = $this->request->get('type', 3);
        $id   = $this->request->get('id');
        $this->view->assign('type', $type);
        $this->view->assign('t_type', 4);

        $where['type'] = $type;
        $train         = M::tableList($where);
        if ($type == 3) {
            # 招生简章
            return $this->fetch('/train/list', [
                'data' => Db::name("train_article")->where([
                    'type'        => $type,
                    'delete_time' => null,
                ])->find(),
            ]);
        } elseif ($type == 6) {//培训老师
            // $train = Db::name("train_article")->where(['type'=>$type])->select();
            $this->view->assign('data', $train);
            return $this->view->fetch('/train/techer');
        } elseif ($type == 1) {
            # 培训活动报道
            return $this->fetch('/train/huodong', [
                'data' => M::where([
                    'delete_time' => null,
                    'type'        => $type,
                ])->order('id desc')->paginate(10, false, ['page' => input('p', 1)]),
            ]);
        } elseif ($type == 7) {
            # 报名
            return $this->fetch('/train/baoming', [
                'data' => Db::name("train_article")->where([
                    'id' => $id,
                ])->find(),
            ]);
        } elseif ($type == 8) {
            # 在线培训
            return $this->fetch('/train/zxpx', [
                'data' => M::where([
                    'delete_time' => null,
                    'type'        => $type,
                ])->order('id desc')->paginate(10, false, ['page' => input('p', 1)]),
            ]);
        } elseif ($type == 5) {//关于同学会
            $this->view->assign('t_type', 5);
            $train = Db::name("train_article")->where(['type' => $type])->find();
            $this->view->assign('data', $train);
            return $this->view->fetch('/train/gytxh');
        } elseif ($type == 2) {//关于同学会精英普
            $this->view->assign('t_type', 5);
            //$train = Db::name("train_article")->where(['type'=>$type])->select();
            $this->view->assign('data', $train);
            return $this->view->fetch('/train/txhjyp');
        } elseif ($type == 4) {
            # 同学会活动报道
            $train = Db::name("train_article")
                ->where('type', 4)
                ->whereNull('delete_time')
                ->order('id desc')
                ->paginate(10);
            return $this->fetch('/train/txhhdbg', [
                't_type' => 5,
                'data'   => $train
            ]);
        } elseif ($type == 10) {//同学会百名
            $this->view->assign('t_type', 5);
            // $train = Db::name("train_article")->where(['type'=>$type])->select();
            $this->view->assign('data', $train);
            return $this->view->fetch('/train/txhbm');
        }
    }

    public function detail()
    {
        $type   = $this->request->get('type', 8);
        $t_type = $this->request->get('t_type', 4);
        $this->view->assign('t_type', $t_type);
        $article_id = $this->request->get('article_id', 8);
        if ($type == 8) {
            # 在线培训
            return $this->fetch('/train/zxpxd', [
                'data' => Db::name("train_article")->where([
                    'type' => $type,
                    'id'   => $article_id,
                ])->find(),
            ]);
        }
        if ($type == 6) {//培训教师
            $train = Db::name("train_article")->where(['type' => $type, 'id' => $article_id])->find();
            $this->view->assign('data', $train);
            return $this->view->fetch('/train/pxdetail');
        }
        if ($type == 1) {
            # 培训活动报道
            return $this->fetch('/train/pxdetailone', [
                'data' => Db::name("train_article")->where([
                    'type' => $type,
                    'id'   => $article_id,
                ])->find(),
            ]);
        }
        if ($type == 5 || $type == 2 || $type == 4) {
            $data = Db::name("train_article")->where(['type' => $type, 'id' => $article_id])->find();
            return $this->fetch('/train/txhdetail', [
                't_type' => 5,
                'data'   => $data
            ]);
        }
    }

    /**
     * @return void
     * 报名申请
     */
    public function trainEnrollAdd()
    {
        $user_id    = $this->auth->id;
        $name       = $this->request->post('name');
        $article_id = $this->request->post('article_id');
        $age        = $this->request->post('age');
        $duties     = $this->request->post('duties');
        $tel        = $this->request->post('tel');
        $mail       = $this->request->post('mail');
        $address    = $this->request->post('address');
        $company    = $this->request->post('company');
        if (!$name || !$age || !$duties || !$tel || !$address || !$company || !$article_id) $this->error("缺少必传参数");

        if ($tel && !Validate::regex($tel, "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        $add_data['uid']        = $user_id;
        $add_data['article_id'] = $article_id;
        $add_data['name']       = $name;
        $add_data['age']        = $age;
        $add_data['duties']     = $duties;
        $add_data['tel']        = $tel;
        $add_data['mail']       = $mail;
        $add_data['address']    = $address;
        $add_data['company']    = $company;
        $add_data['createtime'] = time();
        $add_data['updatetime'] = time();
        $add1                   = Db::name("train_enroll")->insert($add_data);
        if ($add1) {
            $this->success('成功');
        } else {
            $this->error("失败");
        }
    }
}
